﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="volunteer.aspx.cs" MasterPageFile="~/default2.master"
    Inherits="System.Web.UI.Page" %>

<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Net.Mail" %>
<%@ Import Namespace="System.Web.UI" %>

<script type="text/c#" language="CS" runat="server">
    const string WelcomeMessage = " Our staff is a team of volunteers. We would like to donate our time and energy and help kids in rural China. We welcome everyone who would like to lend a helping hand and want to get involved with the work we do at CTEF. Thank you for your loving heart!";
    const string ConfirmMessage = "Thank you for supporting CTEF!";
    const string ErrorMessage = "There has been an error with your submission. Please use 'Back' button to make sure all fields have been filled out. If the error persists, please contact webmaster directly.";

    bool IsValidInput
    {
        get
        {
            return
                !(string.IsNullOrEmpty(txtName.Text.Trim()) ||
                string.IsNullOrEmpty(txtEmail.Text.Trim()) ||
                string.IsNullOrEmpty(txtPhone.Text.Trim()) ||
                string.IsNullOrEmpty(txtOccupation.Text.Trim()) ||
                string.IsNullOrEmpty(chkProjects.SelectedValue) ||
                string.IsNullOrEmpty(radioTime.SelectedValue));
        }        
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            lblMessage.Text = WelcomeMessage;
            pnlInfo.Visible = true;
            btnSubmit.Click += new EventHandler(btnSubmit_Click);
        }
        else
        {
            pnlInfo.Visible = false;
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblMessage.Text = ErrorMessage;
        if (IsValidInput)
        {
            var subject = ComposeEmailSubject();
            var body = ComposeEmailBody();
            
            if (SendEmail(subject, body))
                lblMessage.Text = ConfirmMessage;
        }
   }
    
    string ComposeEmailSubject()
    {
        return "[CTEF volunteer] new signup from " + txtName.Text.Trim();
    }
    
    string ComposeEmailBody()
    {
        var buffer = new StringBuilder();
        buffer.Append("Name: "); buffer.AppendLine(txtName.Text.Trim());
        buffer.Append("Email: "); buffer.AppendLine(txtEmail.Text.Trim());
        buffer.Append("Phone: "); buffer.AppendLine(txtPhone.Text.Trim());
        
        buffer.AppendLine("Interested in projects:");
        foreach (ListItem i in chkProjects.Items)
            if (i.Selected) buffer.AppendFormat("{0}; ", i.Value);

        buffer.AppendLine();
        buffer.AppendFormat("Time commitment: {0}", radioTime.SelectedValue);
        buffer.AppendLine();

        buffer.Append("Occupation: "); buffer.AppendLine(txtOccupation.Text.Trim());
        buffer.Append("Other information: "); buffer.AppendLine(txtMoreInfo.Text.Trim());
        return buffer.ToString();
    }

    bool SendEmail(string mailSubject, string mailBody)
    {
        bool emailSent = false;
        try
        {
            var message = new MailMessage();
            var smtp = new SmtpClient();

            message.From = new MailAddress("china.tomorrow@ctef.org", "China Tomorrow Education Foundation");
            
            // TODO: update to correct email accounts before going live
            message.To.Add(new MailAddress("haonantan@gmail.com"));
            message.To.Add(new MailAddress("yileiw@efura.com"));

            message.IsBodyHtml = false;
            message.Subject = mailSubject;
            message.Body = mailBody;

            smtp.Host = "relay-hosting.secureserver.net";
            smtp.Port = 25;
            smtp.UseDefaultCredentials = false;
            
            // TODO: is there a way to hide plain text credentials?
            smtp.Credentials = null;
            smtp.Send(message);
            emailSent = true;
        }
        catch (Exception ex) { emailSent = false; lblMessage.Text = ex.Message;}
              
        return emailSent;                
    }
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="LeftContent" runat="Server">
    <div id="breadcrumbdiv">
        Get Involved &gt; Volunteer</div>
    <h2>
        BECOME A VOLUNTEER</h2>
    <hr />
    <p>
        <asp:Label ID="lblMessage" runat="server"></asp:Label>
    </p>
    <asp:Panel ID="pnlInfo" runat="server">
        <form name="form_volunteer" runat="server">
        <table width="100%" border="0" align="center" cellpadding="0" cellspacing="1">
            <tr>
                <td width="20%" height="30">
                    Name:
                </td>
                <td width="50%">
                    <asp:TextBox ID="txtName" runat="server" TextMode="SingleLine" Size="30"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td height="30">
                    Email:
                </td>
                <td>
                    <asp:TextBox ID="txtEmail" runat="server" TextMode="SingleLine" Size="30"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td height="30">
                    Telephone:
                </td>
                <td>
                    <asp:TextBox ID="txtPhone" runat="server" TextMode="SingleLine" Size="30"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <p>
                        1. What projects are you interested in?</p>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td align="left">
                    <asp:CheckBoxList ID="chkProjects" runat="server">
                        <asp:ListItem>Help with school projects</asp:ListItem>
                        <asp:ListItem>CTEF  Website updates/maintenance</asp:ListItem>
                        <asp:ListItem>CTEF PR work (newsletters, flyers)</asp:ListItem>
                        <asp:ListItem>Represent CTEF at various events</asp:ListItem>
                    </asp:CheckBoxList>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <p>
                        2. How much of a time commitment can you make?</p>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td align="left">
                    <asp:RadioButtonList ID="radioTime" runat="server">
                        <asp:ListItem>1-2 hours a month</asp:ListItem>
                        <asp:ListItem>3-4 hours a month</asp:ListItem>
                        <asp:ListItem>6-8 hours a month</asp:ListItem>
                        <asp:ListItem>More than 8 hours a month</asp:ListItem>
                    </asp:RadioButtonList>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <p>
                        3. What is your current occupation and position?</p>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:TextBox ID="txtOccupation" runat="server" TextMode="MultiLine" Columns="65"
                        Rows="6" Size="1000"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <p>
                        4. Is there any other information that you would like to share with us (your education
                        background, skills, past volunteer experience, etc.)?</p>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:TextBox ID="txtMoreInfo" runat="server" TextMode="MultiLine" Columns="65" Rows="6"
                        Size="1000"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td height="45" colspan="2" align="center">
                    <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
                </td>
            </tr>
        </table>
        </form>
    </asp:Panel>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="RightContent" runat="Server">
</asp:Content>
